package com.neusoft.race.sys.service.impl;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.neusoft.race.sys.bean.SysDict;
import com.neusoft.race.sys.bean.SysDictItem;
import com.neusoft.race.sys.mappers.SysDictMapper;
import com.neusoft.race.sys.service.ISysDictItemService;
import com.neusoft.race.sys.service.ISysDictService;
import com.neusoft.utils.config.SystemConfig;
import com.neusoft.utils.convert.ConvertUtils;
import com.neusoft.utils.map.MyHashMap;
import com.neusoft.utils.page.PageFactory;
@Service
@Transactional
public class SysDictServiceImpl  implements ISysDictService{
   @Resource
	private SysDictMapper sysDictMapper;
   @Resource
   private ISysDictItemService sysDictItemService;
	@Override
	public   int deleteById(String id) {
		return sysDictMapper.deleteById(id);
	}

	@Override
	public int save(SysDict bean) {
		return sysDictMapper.insert(bean);
	}

	@Override
	public int updateById(SysDict bean) {
		return sysDictMapper.updateById(bean);
	}

	@Override
	public SysDict queryById(String id) {
		return sysDictMapper.queryById(id);
	}

	@Override
	public List<SysDict> queryList(Map<String, Object> param) {
		if(null==param){
			param= new HashMap<String, Object>();
		}
		if(null==param.get("isDelete")){
			param.put("isDelete",0);
		}
		return sysDictMapper.queryList(param);
	}

	@Override
	public Page<SysDict> queryPage(Map<String, Object> param, Pageable page) {
		if(null==param){
			param= new HashMap<String, Object>();
		}
		if(null==param.get("isDelete")){
			param.put("isDelete",0);
		}
		int count=sysDictMapper.queryPageCount(param);
		List<SysDict> list=sysDictMapper.queryPage(param, page);
		return PageFactory.createPage(list, page, count);
	}
	@Override
	public int saveOrUpdate(SysDict bean) {
		if(null==bean){
	     	return -1;
		}
		if(null!=bean.getId()){
		    return sysDictMapper.updateById(bean);
		}
		return sysDictMapper.insert(bean);
	}

	@Override
	public List<SysDictItem> queryDictItemByKey(String key) {
		List<SysDict> sysDicts=queryList(new MyHashMap().set("dictKey", key));
		if(null!=sysDicts&&sysDicts.size()>0){
			SysDict sysDict =sysDicts.get(0);
			List<SysDictItem>  sysDictItems=sysDictItemService.queryList(new MyHashMap().set("dictId", sysDict.getId()));
			return sysDictItems;
		}
		return new ArrayList<SysDictItem>();
	}

	@Override
	public Map<String, String> getDictItemMapByKey(String key) {
		List<SysDictItem>sysDictItems=queryDictItemByKey(key);
		Map<String,String> map= new HashMap<String, String>();
		for (SysDictItem sysDictItem : sysDictItems) {
			map.put(sysDictItem.getItemKey(), sysDictItem.getItemValue());
		}
		return map;
	}

	@Override
	public SysDict getDictByKey(String key) {
		List<SysDict> sysDicts=  queryList(new MyHashMap().set("dictKey", key));
		if(null!=sysDicts&&sysDicts.size()>0){
			return sysDicts.get(sysDicts.size()-1);
		}
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public BigDecimal getSmsUnitPrice() {
		SysDict sysDict =	getDictByKey(SystemConfig.getProperty("sys.key.sms.unitprice"));
		return ConvertUtils.toBigDecimal(sysDict.getDictValue());
	}

}